package com.luck.easyfoodbatch.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.mvp.mine.AddBatchModel;
import com.luck.easyfoodbatch.mvp.mine.AddBatchPresenter;
import com.luck.easyfoodbatch.mvp.mine.AddBatchView;
import com.luck.easyfoodbatch.mvp.model.AddBatch;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.luck.easyfoodbatch.util.T;
import com.luck.easyfoodbatch.util.Tools;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AddSaleAreaActivity extends BaseActivity implements AddBatchView {

    @BindView(R.id.tv_price)
    TextView tv_price;
    @BindView(R.id.tv_area)
    TextView tvArea;
    @BindView(R.id.btn_save_batch)
    Button btnSaveBatch;
    @BindView(R.id.et_unit_price)
    EditText et_unit_price;
    private String gid = "", due_date = "", stock = "", unit = "";
    private AddBatchModel batchModel;
    private String single_price = "";
    private String sold_province = "", price = "", sold_province_zh = "";
    private int type = 0;
    private double mail = 0;
    private String batch_id = "";
    private int position = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        type = getIntent().getIntExtra("type", 0);
        String title = "";
        switch (type) {
            case 1:
                title = "添加售卖地区";
                break;
            case 0:
                title = "编辑售卖地区";
                batch_id = getIntent().getStringExtra("batch_id");
                position = getIntent().getIntExtra("position", -1);
                break;
        }
        sold_province = getIntent().getStringExtra("sold_province");
        sold_province_zh = getIntent().getStringExtra("sold_province_zh");
        price = getIntent().getStringExtra("price");
        setContentView(R.layout.activity_add_sale_area, title, MODE_BACK_NAVIGATION);
        ButterKnife.bind(this);
        mail = getIntent().getDoubleExtra("mail", 0);
        gid = getIntent().getStringExtra("gid");
        due_date = getIntent().getStringExtra("due_date");
        stock = getIntent().getStringExtra("stock");
        unit = getIntent().getStringExtra("unit");
        single_price = getIntent().getStringExtra("single_price");
        if (!Tools.isNull(price)) {
            tv_price.setText(price);
        }
        if (!Tools.isNull(sold_province_zh)) {
            tvArea.setText(sold_province_zh);
        }
        et_unit_price.setText(single_price);
        et_unit_price.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (!Tools.isNull(editable.toString())) {
                    double unit_price = Double.parseDouble(editable.toString());
                    double singe_price = mail * unit_price;
                    tv_price.setText(singe_price + "");
                } else {
                    tv_price.setText("");
                }
            }
        });
    }

    @OnClick({R.id.tv_area, R.id.btn_save_batch})
    public void onClick(View view) {
        Bundle bundle = new Bundle();
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.tv_area:
                intent.setClass(mContext, SaleAreaActivity.class);
                bundle.putString("tempId", sold_province);
                intent.putExtras(bundle);
                startActivityForResult(intent, 200);
                break;
            case R.id.btn_save_batch:
                String price = tv_price.getText().toString().trim();
                String area = tvArea.getText().toString().trim();
                String unit_price = et_unit_price.getText().toString().trim();
                if (Tools.isNull(unit_price)) {
                    T.showToast(mContext, "请输入区域单价");
                } else if (Tools.isNull(price)) {
                    T.showToast(mContext, "请输入价格");
                } else if (Tools.isNull(area)) {
                    T.showToast(mContext, "请选择售卖地区");
                } else {
                    if (area.equals("全国")) {
                        sold_province = "0";
                    }
                    showProgressDialog(getString(R.string.app_loading));
                    batchModel = new AddBatchPresenter(this, mContext);
                    switch (type) {
                        case 0:
                            batchModel.addBatch(type, gid, due_date, stock, sold_province, price, unit_price, batch_id);
                            break;
                        case 1:
                            // 添加批次
                            batchModel.addBatch(type, gid, due_date, stock, sold_province, price, unit_price, "");
                            break;
                    }

                }
                break;
        }
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 200:
                    if (data != null) {
                        sold_province = data.getStringExtra("id");
                        String name = data.getStringExtra("name");
                        if (!Tools.isNull(name)) {
                            tvArea.setText(name);
                        } else {
                            tvArea.setText("全国");
                        }
                    }
                    break;
            }
        }
    }

    @Override
    public void addBatchSuccess(AddBatch model) {
        if (model != null) {
            if (model.getStatus() == HttpUtil.RESULT_OK) {
                AddBatch.DataBean data = model.getData();
                data.setSold_province_zh(tvArea.getText().toString().trim());
                data.setUnit(unit);
                data.setSingle_price(et_unit_price.getText().toString().trim());
                Intent intent = new Intent();
                intent.putExtra("data", data);
                intent.putExtra("position", position);
                intent.putExtra("type", type);
                setResult(RESULT_OK, intent);
                finish();
            } else {
                T.showToast(mContext, model.getMsg());
            }
        }
    }

    @Override
    public void httpStatusError() {

    }

    @Override
    public void hideLoading() {
        hideShowProgress();
    }
}
